/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package at.ac.tuwien.ewa.g70.api;

import java.util.HashMap;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;

/**
 *
 * @author Amras
 */
@ManagedBean(name="UserManager")
@ApplicationScoped
public class UserManager {

    HashMap<String, User> users;

    /** Creates a new instance of UserManager */
    public UserManager() {
        users = new HashMap<String, User>();
    }

    public boolean registerUser(User user) {
        if( users.containsKey(user.getLogin()) ){
            return false;
        }
        else {
            users.put(new String(user.getLogin()), new User(user));
            return true;
        }
    }

    public User getRegisteredUser(String login) {
        return users.get(login);
    }

    public User login(String login, String password){
        User user = getRegisteredUser(login);
        
        if( user == null ) {
            return null;
        }
        else {
            if( user.getPassword().compareTo(password) == 0){
                return user;
            }
            else {
                return null;
            }
        }
    }

    public boolean changeUserdata(User user){
        User userOld = getRegisteredUser(user.getLogin());

        if( userOld == null ) {
            return false;
        }
        else {
            userOld = new User(user);
            return true;
        }
    }
}